package com.itcast.yygh.user.controller;

import com.itcast.common.result.R;
import com.itcast.common.utils.AuthContextHolder;
import com.itcast.yygh.model.model.user.UserInfo;
import com.itcast.yygh.model.vo.user.LoginVo;
import com.itcast.yygh.model.vo.user.UserAuthVo;
import com.itcast.yygh.user.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 用户登录服务
 */

@Api(tags = "用户登录服务")
@RestController
@RequestMapping("/api/userinfo")
@RequiredArgsConstructor
public class UserInfoController {
    private final UserInfoService userInfoService;

    //会员登录接口
    @ApiOperation("会员登录接口")
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo) {
        Map<String, Object> info = userInfoService.login(loginVo);
        return R.ok().data(info);
    }

    //用户认证接口
    @ApiOperation("用户认证接口")
    @PostMapping("auth/userAuth")
    public R userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        //传递两个参数，第一个参数用户id，第二个参数认证数据vo对象
        userInfoService.userAuth(AuthContextHolder.getUserId(request),userAuthVo);
        return R.ok();
    }

    //获取用户id信息接口
    @ApiOperation("获取用户id信息接口")
    @GetMapping("auth/getUserInfo")
    public R getUserInfo(HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return R.ok().data("userInfo",userInfo);
    }
}
